import sys
from bs4 import BeautifulSoup
import urllib.request as urllib2
import urllib

url = 'http://www.doorstore.co.uk/products/internal-interior-doors/oak-doors/641/'

response = urllib2.urlopen(url)
html = response.read()

soup = BeautifulSoup(html)

img_classes = soup.findAll('span', {'class': 'img'})

test_url_backspace = 'http://93.72.16.48/door-market/productimg/door 1 lar.jpg'

print(test_url_backspace)

test_url_without_backspace = test_url_backspace.replace(' ','%20')

print(test_url_without_backspace)

test_url =  'http://www.doorstore.co.uk/ProdImages/Contemporary%204%20Lite.jpg'
image_on_web = urllib2.urlopen(test_url)

print(image_on_web)
buf = image_on_web.read()

downloaded_image = open('test34.jpg', "wb")
downloaded_image.write(buf)
downloaded_image.close()
image_on_web.close()

print('*************************************************************')


i=0
for im in img_classes:
	src = im.find('img').get('src')
	print('---------------------------------------------------------------------')
	print(src)

	file_name = src.replace('/ProdImages/','')
	file_name = file_name.replace('/prodimages/','')
	
	
	
	url_path = 'http://www.doorstore.co.uk'
	full_url = url_path + src
	full_url = full_url.replace(' ','%20')
	print(full_url)
	print(file_name)
	#if i == 0:
	image_on_web = urllib2.urlopen(full_url)
	buf = image_on_web.read()

	downloaded_image = open(file_name, "wb")
	downloaded_image.write(buf)
	downloaded_image.close()
	image_on_web.close()

 #    buf = image_on_web.read()
 #    downloaded_image = file(str(i)+'.jpg', "wb")
 #    downloaded_image.write(buf)
 #    downloaded_image.close()
 #    image_on_web.close()
    #i = 1
	print('---------------------------------------------------------------------')
#print(table)

# for link in soup.find_all('img'):
# 	cur_image_path = link.get('src')
#     print(cur_image_path)
#print(html)

print('12')